package com.burst.reptile.modular.zp.service.impl;

import java.util.List;

import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.burst.reptile.common.constant.UserConstants;
import com.burst.reptile.common.exception.CustomException;
import com.burst.reptile.common.utils.ToolUtil;
import com.burst.reptile.modular.zp.entity.ZpServiceItems;
import com.burst.reptile.modular.zp.mapper.ZpServiceItemsMapper;
import com.burst.reptile.modular.zp.service.IZpServiceItemsService;

import cn.hutool.core.util.StrUtil;

/**
 * <p>
 * 服务项目列表 服务实现类
 * </p>
 *
 * @author wujin
 * @since 2020-06-23
 */
@Service
public class ZpServiceItemsServiceImpl extends ServiceImpl<ZpServiceItemsMapper, ZpServiceItems> implements IZpServiceItemsService {

	@Override
	public List<ZpServiceItems> listAll(ZpServiceItems serviceItems) {
		QueryWrapper<ZpServiceItems> queryWrapper = new QueryWrapper<>();
		String name = serviceItems.getName();
		if(StrUtil.isNotEmpty(name)) {
			queryWrapper.like("name", name);
		}
		return this.list(queryWrapper);
	}

	@Override
	public String checkServiceItemsKeyUnique(ZpServiceItems serviceItems) {
		Integer sid = ToolUtil.isEmpty(serviceItems.getSid()) ? -1 : serviceItems.getSid();
		ZpServiceItems info = this.baseMapper.selectOne(new QueryWrapper<ZpServiceItems>().eq("name", serviceItems.getName()));
		if (ToolUtil.isNotEmpty(info) && info.getSid().intValue() != sid.intValue()) {
			return UserConstants.NOT_UNIQUE;
		}
		return UserConstants.UNIQUE;
	}

	@Override
	public int insertServiceItems(ZpServiceItems serviceItems) {
		return this.baseMapper.insert(serviceItems);
	}

	@Override
	public int updateServiceItems(ZpServiceItems serviceItems) {
		Integer sid = serviceItems.getSid();
		if(ToolUtil.isEmpty(sid)) {
			throw new CustomException("修改失败，缺少参数");
		}
		ZpServiceItems byId = this.getById(sid);
		if(ToolUtil.isEmpty(byId)) {
			throw new CustomException("修改失败，资源不存在");
		}
		return this.baseMapper.updateById(serviceItems);
	}

	@Override
	public int deleteByIds(Integer[] ids) {
		return this.baseMapper.deleteColumnByIds(ids);
	}

}
